package br.com.cogerh.sidec.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.cogerh.sidec.model.CompromissoConcluido;
import br.com.cogerh.sidec.util.HibernateUtil;

public class CompromissoConcluidoImpl implements CompromissoConcluidoDAO {

	@Override
	public void salvar(CompromissoConcluido compromissoConcluido) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		try{
			sessao.save(compromissoConcluido);
			sessao.flush();
			sessao.close();
			tx.commit();
		}catch(HibernateException e){
			tx.rollback();
			
		}
	

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CompromissoConcluido> listar() {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();

		List<CompromissoConcluido> lista = new ArrayList<CompromissoConcluido>();

		try{
		Criteria criteria = sessao.createCriteria(CompromissoConcluido.class);
		lista = criteria.list();
		sessao.close();
		}catch(HibernateException e){
		tx.rollback();
		}
		
		return lista;

	}

	@Override
	public void editar(CompromissoConcluido compromissoConcluido) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		try{
			sessao.merge(compromissoConcluido);
			sessao.flush();
			tx.commit();
		}catch(HibernateException e){
			tx.rollback();
		}
		

	}

	@Override
	public void excluir(CompromissoConcluido compromissoConcluido) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		try{
			sessao.delete(compromissoConcluido);
			sessao.flush();
			tx.commit();
		}catch(HibernateException e){
			tx.rollback();
		}
		
	
	}

	@Override
	public void fecharSessao() {
	

	}

}
